<?php
// +----------------------------------------------------------------------
// | 功能说明"
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;

class SysconfigModel extends Model
{
    
    protected $name = 'sys_config';
    protected $autoWriteTimestamp = true;//时间自动更新 create_time ,update_time


    public $title = '名称';



    
   
    public function edit($param)
    {
        try{
          
            $result =  $this->update($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('sysuser/index'), '修改用户成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }


    public function add($param)
    {
        try{
          
            $result =  $this->save($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('sysuser/index'), '添加用户成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }
    public function getById($id)
    {
        return $this->where('id', $id)->find();
    }
    
    public function delById($id)
    {
        try{
            $this->where('id', $id)->update(['isdelete'=>'1']);
            return msg(1, '', '删除成功');
        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

  
    public function getByName($name)
    {
        return $this->where('name', $name)->find();
    }
    

    

    /**
     * 系统配置信息
     * @param $param
     */
    public function insertConfig($param)
    {
        try{
            if(empty($this->getByName($param['name'])))
            {
                $result =  $this->save($param);
            }
            else
            {
                $result =  $this->allowField(['value'])->save(['value'=>$param['value']],['name'=>$param['name']]);
            }
         
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                return msg(1, '', '');
            }
        } catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }


}
